﻿Imports System
Imports System.Data
Imports System.Data.Odbc
Imports System.Data.Odbc.OdbcType

Namespace Framework
    Public NotInheritable Class ODBCConnections
        Public Enum ActionType As Integer
            [Select] = 0
            Insert = 1
            Update = 2
        End Enum

        Public Shared Cmd As System.Data.Odbc.OdbcCommand
        Public Shared Cnn As System.Data.Odbc.OdbcConnection
        Public Shared Tran As System.Data.Odbc.OdbcTransaction

        Public Shared ConnectionString As String = Framework.SystemConfiguration.ConnectionString

        Public Shared Sub OdbcConnectionOpen()
            If ODBCConnections.Cnn Is Nothing Then ODBCConnections.Cnn = New System.Data.Odbc.OdbcConnection
            With ODBCConnections.Cnn
                If .State = ConnectionState.Closed Then
                    .ConnectionString = ODBCConnections.ConnectionString
                    .Open()
                End If
            End With
        End Sub

        Public Shared Sub OdbcBeginTransaction()
            If ODBCConnections.Cnn Is Nothing Then ODBCConnections.Cnn = New System.Data.Odbc.OdbcConnection
            With ODBCConnections.Cnn
                If .State = ConnectionState.Closed Then
                    .ConnectionString = ODBCConnections.ConnectionString
                    .Open()
                    ODBCConnections.Tran = .BeginTransaction
                End If
            End With
        End Sub

        Public Shared Function OdbcConnectionOpen(ByVal cnn As System.Data.Odbc.OdbcConnection) As System.Data.Odbc.OdbcConnection
            If cnn Is Nothing Then cnn = New System.Data.Odbc.OdbcConnection
            With cnn
                If .State = ConnectionState.Closed Then
                    .ConnectionString = ""
                    .Open()
                End If
            End With
            Return cnn
        End Function

        Public Shared Function OdbcBeginTransaction(ByVal cnn As System.Data.Odbc.OdbcConnection) As System.Data.Odbc.OdbcConnection
            If cnn Is Nothing Then cnn = New System.Data.Odbc.OdbcConnection
            With cnn
                If .State = ConnectionState.Closed Then
                    .ConnectionString = ""
                    .Open()
                    ODBCConnections.Tran = .BeginTransaction
                End If
            End With
            Return cnn
        End Function

        Public Shared Function OdbcBeginTransaction(ByVal cnn As System.Data.Odbc.OdbcConnection, ByVal tran As System.Data.Odbc.OdbcTransaction) As System.Data.Odbc.OdbcConnection
            If cnn Is Nothing Then cnn = New System.Data.Odbc.OdbcConnection
            With cnn
                If .State = ConnectionState.Closed Then
                    .ConnectionString = ""
                    .Open()
                    tran = .BeginTransaction
                End If
            End With
            Return cnn
        End Function

        Public Shared Sub DisposeOdbcConnection(ByVal cnn As System.Data.Odbc.OdbcConnection)
            If Not cnn Is Nothing Then
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                cnn.Dispose()
            End If
        End Sub

        Public Shared Sub DisposeOdbcConnection(ByVal cmd As System.Data.Odbc.OdbcCommand)
            If Not cmd Is Nothing Then
                If Not cmd.Connection Is Nothing Then
                    If cmd.Connection.State = ConnectionState.Open Then
                        cmd.Connection.Close()
                    End If
                    cmd.Connection.Dispose()
                End If
                cmd.Dispose()
            End If
        End Sub

        Public Shared Sub DisposeOdbcConnection(ByVal adapter As System.Data.Odbc.OdbcDataAdapter)
            If Not adapter Is Nothing Then
                If Not adapter.SelectCommand Is Nothing Then
                    If Not adapter.SelectCommand.Connection Is Nothing Then
                        If Not adapter.SelectCommand.Connection.State = ConnectionState.Open Then
                            adapter.SelectCommand.Connection.Close()
                        End If
                        adapter.SelectCommand.Connection.Dispose()
                    End If
                    adapter.SelectCommand.Dispose()
                End If
                adapter.Dispose()
            End If
        End Sub

        Public Shared Sub ClearParameterObject(ByVal cmd As System.Data.Odbc.OdbcCommand)
            If cmd.Parameters.Count > 0 Then
                cmd.Parameters.Clear()
            End If
        End Sub

        Public Shared Sub ClearParameterObject(ByVal type As ActionType, ByVal adapter As System.Data.Odbc.OdbcDataAdapter)
            Select Case type
                Case ActionType.Select
                    If adapter.SelectCommand.Parameters.Count > 0 Then
                        adapter.SelectCommand.Parameters.Clear()
                    End If
                Case ActionType.Insert
                    If adapter.InsertCommand.Parameters.Count > 0 Then
                        adapter.InsertCommand.Parameters.Clear()
                    End If
                Case ActionType.Update
                    If adapter.UpdateCommand.Parameters.Count > 0 Then
                        adapter.UpdateCommand.Parameters.Clear()
                    End If
            End Select
        End Sub
    End Class
End Namespace